package com.doublefish.doublefishrobot.ui.home.s6;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.content.res.AppCompatResources;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.ui.home.path.DFPingPongTableView;
import com.doublefish.doublefishrobot.ui.home.widget.ModeHeaderIndexView;
import com.doublefish.doublefishrobot.ui.home.widget.ModeSettingItemLoopContentView;
import com.doublefish.doublefishrobot.ui.home.widget.ModeSettingItemValuePopView;
import com.doublefish.doublefishrobot.ui.video.VideoPlayerActivity;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;
import com.doublefish.doublefishrobot.utils.DebounceOnClickListener;
import com.doublefish.doublefishrobot.utils.ble.DFBleManager;
import com.doublefish.doublefishrobot.utils.ble.DFS6ProtocolManager;
import com.doublefish.doublefishrobot.utils.ble.bean.ModeConstant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2024/10/24 10:39
 */
public class S6ModeTrainLoopActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView titleTV;

    @BindView(R.id.pingpong_view)
    DFPingPongTableView pingPongTableView;

    @BindView(R.id.view_index)
    ModeHeaderIndexView indexView;

    @BindView(R.id.mode_loop_content)
    ModeSettingItemLoopContentView loopContentView;

    @BindView(R.id.tv_custom)
    TextView customTV;

    @BindView(R.id.tv_test)
    TextView testTV;

    @BindView(R.id.tv_train)
    TextView trainTV;

    private ModeSettingBean modeSettingBean;
    //初始化 循环训练参数
    private ModeSettingBean.ModeSettingLoopBean loopBean = ModeSettingBean.ModeSettingLoopBean.defaultLoop(ModeConstant.ProtocolTypeS6);;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mode_loop_s8;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        modeSettingBean = intent.getParcelableExtra("mode");

        String modeName = modeSettingBean.getName();
        titleTV.setText(modeName);

        reloadData();

        indexView.setTotalSize(modeSettingBean.getSteps().size());

        ModeSettingBean.ModeSettingStepBean stepBean = modeSettingBean.getSteps().get(0);
        pingPongTableView.setS6StepBean(stepBean);

        if (modeSettingBean.isMoreThan2Range()) {
            customTV.setEnabled(false);
            customTV.setBackground(AppCompatResources.getDrawable(this,R.drawable.shape_grey_22));
        }
    }

    @Override
    protected void initListener() {
        indexView.setEditAble(false);
        indexView.setOnModeHeaderIndexViewListner(new ModeHeaderIndexView.OnModeHeaderIndexViewListner() {
            @Override
            public void onIndexChanged(int index) {
                ModeSettingBean.ModeSettingStepBean stepBean = modeSettingBean.getSteps().get(index);
                pingPongTableView.setS6StepBean(stepBean);
            }

            @Override
            public void onAddStep() {

            }
        });

        loopContentView.setOnModeSettingItemLoopContentViewListner(new ModeSettingItemLoopContentView.OnModeSettingItemLoopContentViewListner() {
            @Override
            public void onModeSettingItemValueClick(ModeSettingBean.ModeSettingLoopBean loopBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag) {
                showItemValuePopView(loopBean ,itemBean, itemTag);
            }
        });

        testTV.setOnClickListener(new DebounceOnClickListener() {
            @Override
            protected void onClickView(View view) {
                testBallBtnClick();
            }
        });

        trainTV.setOnClickListener(new DebounceOnClickListener() {
            @Override
            protected void onClickView(View view) {
                trainBtnClick();
            }
        });
    }

    @OnClick(R.id.ll_back)
    public void onBackClick() {
        finish();
    }

    @OnClick({R.id.tv_custom, R.id.tv_video})
    public void onBtnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_custom: {
                customBtnClick();
            }
            break;
            case R.id.tv_video: {
                videoBtnClick();
            }
            break;

            default:
                break;
        }
    }

    private void reloadData() {
        loopContentView.setSettingLoopBean(loopBean);
    }

    private void showItemValuePopView(ModeSettingBean.ModeSettingLoopBean loopBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag) {
        if (itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount
                || itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime) {
            List<ModeSettingBean.ModeSettingItemBean> itemBeans = new ArrayList<>();
            itemBeans.add(loopBean.getLoopCountItemBean());
            itemBeans.add(loopBean.getLoopTimeItemBean());

            List<String> itemMsgs = new ArrayList<>();
            itemMsgs.add(loopBean.getLoopCountItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount));
            itemMsgs.add(loopBean.getLoopTimeItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime));

            int index = itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount ? 0 : 1;
            ModeSettingItemValuePopView popView = new ModeSettingItemValuePopView(this, itemBeans, itemMsgs, index, new ModeSettingItemValuePopView.OnSettingItemValuePopViewListner() {
                @Override
                public void onValueFinish(ModeSettingBean.ModeSettingItemBean itemBean, int index) {
                    loopBean.setLoopType(index);
                    reloadData();
                }

                @Override
                public void onValueError(String error) {
                    showToast(error);
                }

                @Override
                public void onCancelBtnClick() {

                }
            });
            popView.show();

        } else {
            String itemMsg = itemBean.getDescWithItemTag(itemTag);
            ModeSettingItemValuePopView popView = new ModeSettingItemValuePopView(this, itemBean, itemMsg, new ModeSettingItemValuePopView.OnSettingItemValuePopViewListner() {
                @Override
                public void onValueFinish(ModeSettingBean.ModeSettingItemBean itemBean, int index) {
                    reloadData();
                }

                @Override
                public void onValueError(String error) {
                    showToast(error);
                }

                @Override
                public void onCancelBtnClick() {

                }
            });
            popView.show();
        }
    }


    private void customBtnClick() {
        Intent intent = new Intent(this, S6ModeCustomActivity.class);
        intent.putExtra("mode", modeSettingBean);
        startActivity(intent);
    }

    private void videoBtnClick() {
        Log.i("ModeTrainLoopActivity","videoBtnClick");
        if (AppCacheUtil.getInstance().isStoragePermissionEnable()) {
            Intent intent = VideoPlayerActivity.createIntent(this, modeSettingBean.getName(), modeSettingBean.getCode());
            startActivity(intent);
        } else {
            checkStoragePermission();
        }
    }

    private void testBallBtnClick() {
        Log.i("ModeTrainLoopActivity","testBallBtnClick");
        if (!DFBleManager.getInstance().isDidConnectBlueTooth()) {
            showToast(getString(R.string.connect_ble_first));
            return;
        }

        if (DFBleManager.getInstance().curDeviceIsProtocolTypeS6()) {
            //测试球 默认测试一次 按次数运行脚本1次
            int dir = (int) modeSettingBean.getDifficulty() + 1;
            //pragma mark -- 2024-3-5 超6的文件名去掉汉字名称，只要编码
            // String fileName = modeSettingBean.getCode() + modeSettingBean.getName();
            String fileName = modeSettingBean.getCode() ;

            int loopInterval = 1000; //单位ms
            boolean timeOrCount = false;
            int time = 0;
            int count = 1;
            //运行脚本
            DFS6ProtocolManager.getInstance().runScript(dir, fileName, loopInterval, timeOrCount, time, count);
        }
    }

    private void trainBtnClick() {
        Log.i("ModeTrainLoopActivity","trainBtnClick");
        if (!DFBleManager.getInstance().isDidConnectBlueTooth()) {
            showToast(getString(R.string.connect_ble_first));
            return;
        }

        int dir = modeSettingBean.getDifficulty() + 1;
        //pragma mark -- 2024-3-5 超6的文件名去掉汉字名称，只要编码
//        String fileName = modeSettingBean.getCode() + modeSettingBean.getName();
        String fileName = modeSettingBean.getCode() ;

        boolean isTime = (loopBean.getLoopType() == 1);
        int count = loopBean.getLoopCountItemBean().getCurrentValue();

        //mark：--  2024-1-4 发球时间 和 发球间隔
        //单位分钟
        int loopMinute = loopBean.getLoopTimeItemBean().getCurrentValue();
        //单位是秒
        int time =  loopMinute * 60;

        //间隔 单位秒
        int loopIntervalSecond = loopBean.getLoopIntervalItemBean().getCurrentValue();
        //间隔精确到ms
        int loopInterval = loopIntervalSecond * 1000;

//        Intent intent = new Intent(this, S6ModeBallActivity.class);
//        intent.putExtra("modeName", modeSettingBean.getName());
//        intent.putExtra("isSystem", true);
//        intent.putExtra("dirCode", dir);
//        intent.putExtra("fileName", fileName);
//        intent.putExtra("loopInterval", loopInterval);
//        intent.putExtra("isTime", isTime);
//        intent.putExtra("time", time);
//        intent.putExtra("count", count);
//        startActivity(intent);

        Intent intent = S6ModeBallActivity.createInnerIntent(this,
                modeSettingBean.getName(),
                true,
                dir,
                fileName,
                loopInterval,
                isTime,
                time,
                count);
        startActivity(intent);
    }

    @Override
    public void onStoragePermission() {
        super.onStoragePermission();

        Intent intent = VideoPlayerActivity.createIntent(this, modeSettingBean.getName(), modeSettingBean.getCode());
        startActivity(intent);
    }
}
